﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EITSurferInfinityLib
{
    public class ElectrodeErrorsDetection
    {
        double[] Impdance;
        byte[] ChannelState;
        public byte[] ElectrodeState;

        int Offset;
        // 0 = larger than ThresholdBad
        // 1 = larger between ThreshoBad <nd ThreshoGood
        // 2 = smaller or equal than ThresholdGood
        // 3 = unknow

        double ThresholdGood;
        double ThresholdBad;

        public ElectrodeErrorsDetection()
        {
            ThresholdGood = 200;
            ThresholdBad = 600;
        }

        public void SetOffset(int offset)
        {
            Offset = offset;
        }
        public void SetImpedanceData(double [] inData)
        {
            Impdance= new double[inData.Length];
            for (int i = 0; i > Impdance.Length; i++ )
            {
                Impdance[i] = inData[i];
            }
        }

        public void CalculateElectrodeState()
        {
            ElectrodeState = new byte[Impdance.Length];
            if (NumberOfBadlyConnectedChannels() <= 2)
            {
                int badel = SingleError();
                for (int i = 0; i < ChannelState.Length; i++)
                {
                    if (i == badel)
                    {
                        ElectrodeState[i] = 0;
                    }
                }
            }
            else
            {
                for (int i = 0; i < ChannelState.Length; i++)
                {
                    if (ChannelState[i] == 0)
                    {
                        ElectrodeState[i] = 3;
                    }
                }
            }
        }

        public int SingleError()
        {
            int index1 = -1;
            int index2 = -1;
            for (int i = 0; i < ChannelState.Length; i++)
            {
                if (ChannelState[i] == 0)
                {
                    if (index1 == -1)
                    {
                        index1 = i;
                    }
                    else
                    {
                        index2 = i;
                    }
                }
            }

            if ((index2 - index1) == Offset)
            {
                return index2;
            }
            else
            {
                return index1;
            }
        }
        public int NumberOfBadlyConnectedChannels()
        {
            int counter = 0;
            ChannelState= new byte[Impdance.Length];
            for (int i = 0; i < Impdance.Length; i++)
            {
                if (Impdance[i] < ThresholdGood)
                {
                    ChannelState[i] = 2;
                }
                else
                {
                    if (Impdance[i] < ThresholdBad)
                    {
                        ChannelState[i] = 1;
                    }
                    else
                    {
                        ChannelState[i] = 0;
                    }
                }
            }

            for (int i = 0; i < Impdance.Length; i++)
            {
                if ((ChannelState[i] == 2) || (ChannelState[i] == 1))
                {
                    counter++;
                }
            }

            return counter;
        }
    }
}
